using System;
using System.Collections.Generic;

namespace TNet
{
    public class Pool<T> where T : new()
    {
        static public uint MaxNum = uint.MaxValue;
        static List<T> mPool = new List<T>();

        static public T Create()
        {
            T t = default(T);

            if (mPool.size == 0)
            {
                t = new T();
            }
            else
            {
                lock (mPool)
                {
                    if (mPool.size != 0)
                    {
                        t = mPool.Pop();
                    }
                    else
                    {
                        t = new T();
                    }
                }
            }

            return t;
        }

        static public void Recycle(T t)
        {
            if (mPool.size < MaxNum)
            {
                lock (mPool)
                {
                    mPool.Add(t);
                }
            }
        }
    }
}